package com.lzb.randomturntable.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.basic.dialog.BaseSmartDialog;
import com.lzb.basic.utils.SPUtil;
import com.lzb.randomturntable.Content;
import com.lzb.randomturntable.R;
import com.lzb.randomturntable.adapter.CoinAdapter;
import com.lzb.randomturntable.bean.CoinBean;
import com.lzb.randomturntable.databinding.DialogBombSettingBinding;
import com.lzb.randomturntable.databinding.DialogCoinSettingBinding;

import java.util.ArrayList;
import java.util.List;


public class CoinSettingDialog extends BaseSmartDialog<DialogCoinSettingBinding> implements View.OnClickListener {

    private CoinAdapter coinAdapter;
    private int selPosition;

    public CoinSettingDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_coin_setting;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.ivCancel.setOnClickListener(this);

        selPosition = SPUtil.getInt(getContext(), Content.coinPos, 0);

        List<CoinBean> list = new ArrayList<>();
        list.add(new CoinBean(R.drawable.yb1, R.drawable.yb2));
        list.add(new CoinBean(R.drawable.yb3, R.drawable.yb4));
        list.add(new CoinBean(R.drawable.yb5, R.drawable.yb6));
        list.add(new CoinBean(R.drawable.yb7, R.drawable.yb8));

        list.get(selPosition).setSelected(true);

        mDataBinding.rvCoinList.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        coinAdapter = new CoinAdapter();
        mDataBinding.rvCoinList.setAdapter(coinAdapter);
        coinAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                coinAdapter.getItem(selPosition).setSelected(false);
                selPosition = position;
                coinAdapter.getItem(position).setSelected(true);
                coinAdapter.notifyDataSetChanged();
                SPUtil.putInt(getContext(), Content.coinPos, position);
                if (listener != null) {
                    listener.clickItem();
                }
            }
        });
        coinAdapter.setList(list);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ivCancel) {
            dismiss();
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickItem();
    }
}
